#!/usr/bin/env python
# coding=utf-8

import argparse
import sys
import os

parser = argparse.ArgumentParser(
    description='''
    Gemoma的gff提取出来 特别是当其出现失败的情况下
    用法:
    fetch_Gemoma_gff.py -i GeMoMa_temp -o result.gff
    ''')




parser.add_argument('-i',
                help='必须给定，输入的gemoma_temp目录')


parser.add_argument('-o',
                help='必须给定，输出文件')


# 取出pasa中被 merge的 mRNA

args = parser.parse_args()

if not args.i or not args.o:
    parser.print_help()
    sys.exit()



infile = args.i
outfile = args.o 

outfile = open(outfile,'w')

k = os.listdir(infile)
if len(k)>0:
	infile = infile + '/' + k[0]

else:
	raise BaseException('file not exist')


zero_path = infile +'/0'

lista = []

if os.path.exists(zero_path) and os.path.isdir(zero_path):
	for i in os.listdir(zero_path):
		if i.isnumeric():
			target_path = zero_path+'/'+i+'/predicted_annotation.gff' 
			with open(target_path) as fila:
				for i in fila:
					k = i.strip().split('#')[0].split('\t')
					if len(k)>3:
						lista.append(k)

for i in lista:
	outfile.write('\t'.join(i)+'\n')

outfile.close()


